//
//  JavaCalendar.m
//
//  Created by GaoShin on 2020/6/28.
//

#import "JavaCalendar.h"

@implementation JavaCalendar
{
    NSCalendar * calendar;
    NSDate * internalDate;
}

-(instancetype) initWithNSDate:(NSDate *) date{
    self = [super init];
    if(self){
        self->calendar = [NSCalendar currentCalendar];
        self->internalDate = [date copy];
    }
    return self;
}

-(instancetype) initWithJavaDate:(JavaDate *) javaDate{
    self = [super init];
    if(self){
        self->calendar = [NSCalendar currentCalendar];
        self->internalDate = [javaDate getDate];
    }
    return self;
}

-(instancetype) initWithTimeInMillis:(long long) time{
    self = [super init];
    if(self){
        self->calendar = [NSCalendar currentCalendar];
        NSTimeInterval tempMilli = time;
        NSTimeInterval seconds = tempMilli/1000.0;//这里的.0一定要加上，不然除下来的数据会被截断导致时间不一致
        self->internalDate = [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return self;
}

-(instancetype) init {
    self = [super init];
    if(self){
        self->calendar = [NSCalendar currentCalendar];
        self->internalDate = [NSDate date];
    }
    return self;
}

-(JavaDate *) getTime{
    return [JavaDate javaDateFromNSDate:internalDate];
}

-(long long) getTimeInMillis{
    NSTimeInterval interval = [internalDate timeIntervalSince1970];
    long long totalMilliseconds = interval*1000 ;
    return totalMilliseconds;
}

-(void) setYear:(NSUInteger) year month:(NSUInteger) month day:(NSUInteger) day{
    NSDateComponents * components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month+1;
    components.day = day;
    internalDate = [calendar dateFromComponents:components];
}

-(void) setYear:(NSUInteger) year month:(NSUInteger) month day:(NSUInteger) day hourOfDay:(NSUInteger) hourOfDay minute:(NSUInteger) minute{
    NSDateComponents * components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month+1;
    components.day = day;
    components.hour = hourOfDay;
    components.minute = minute;
    internalDate = [calendar dateFromComponents:components];
}

-(void) setYear:(NSUInteger) year month:(NSUInteger) month day:(NSUInteger) day hourOfDay:(NSUInteger) hourOfDay minute:(NSUInteger) minute second:(NSUInteger) second{
    NSDateComponents * components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month+1;
    components.day = day;
    components.hour = hourOfDay;
    components.minute = minute;
    components.second = second;
    internalDate = [calendar dateFromComponents:components];
}

-(void) setJavaDate:(JavaDate *) date{
    internalDate = [date getDate];
}

-(void) setNSDate:(NSDate *) date{
    internalDate = date;
}

-(void) setTimeInMillis:(long) millis{
    NSTimeInterval tempMilli = millis;
    NSTimeInterval seconds = tempMilli/1000.0;//这里的.0一定要加上，不然除下来的数据会被截断导致时间不一致
    internalDate = [NSDate dateWithTimeIntervalSince1970:seconds];
}

-(void) setField:(JavaCalendarFieldType) fieldType withValue:(NSUInteger) value{
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents * components = [calendar components:unitFlags fromDate:internalDate];
    switch (fieldType) {
        case JavaCalendarFieldTypeYear:
            components.year = value;
            break;
        case JavaCalendarFieldTypeMonth:
            components.month = value+1;
            break;
        case JavaCalendarFieldTypeDate:
            components.day = value;
            break;
        case JavaCalendarFieldTypeHourOfDay:
            components.hour = value;
            break;
        case JavaCalendarFieldTypeMinute:
            components.minute = value;
            break;
        case JavaCalendarFieldTypeSecond:
            components.second = value;
            break;
    }
    internalDate = [calendar dateFromComponents:components];
}

-(NSUInteger) getFieldValue:(JavaCalendarFieldType) fieldType{
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents * components = [calendar components:unitFlags fromDate:internalDate];
    switch (fieldType) {
        case JavaCalendarFieldTypeYear:
            return components.year;
        case JavaCalendarFieldTypeMonth:
            return components.month-1;
        case JavaCalendarFieldTypeDate:
            return components.day;
        case JavaCalendarFieldTypeHourOfDay:
            return components.hour;
        case JavaCalendarFieldTypeMinute:
            return components.minute;
        case JavaCalendarFieldTypeSecond:
            return components.second;
    }
}

-(void) field:(JavaCalendarFieldType) fieldType operationAdd:(NSInteger) value{
    NSDateComponents * components = [[NSDateComponents alloc] init];
    switch (fieldType) {
        case JavaCalendarFieldTypeYear:
            components.year = value;
            break;
        case JavaCalendarFieldTypeMonth:
            components.month = value;
            break;
        case JavaCalendarFieldTypeDate:
            components.day = value;
            break;
        case JavaCalendarFieldTypeHourOfDay:
            components.hour = value;
            break;
        case JavaCalendarFieldTypeMinute:
            components.minute = value;
            break;
        case JavaCalendarFieldTypeSecond:
            components.second = value;
            break;
    }
    internalDate = [calendar dateByAddingComponents:components toDate:internalDate options:NSCalendarMatchStrictly];
}

-(NSUInteger) getActualMinimum:(JavaCalendarFieldType) fieldType{
    NSRange range;
    switch (fieldType) {
        case JavaCalendarFieldTypeYear:
            range = NSMakeRange(1970, 2100);
            break;
        case JavaCalendarFieldTypeMonth:
            range = [calendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:internalDate];
            break;
        case JavaCalendarFieldTypeDate:
            range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:internalDate];
            break;
        case JavaCalendarFieldTypeHourOfDay:
            range = [calendar rangeOfUnit:NSCalendarUnitHour inUnit:NSCalendarUnitDay forDate:internalDate];
            break;
        case JavaCalendarFieldTypeMinute:
            range = [calendar rangeOfUnit:NSCalendarUnitMinute inUnit:NSCalendarUnitHour forDate:internalDate];
            break;
        case JavaCalendarFieldTypeSecond:
            range = [calendar rangeOfUnit:NSCalendarUnitSecond inUnit:NSCalendarUnitMinute forDate:internalDate];
            break;
    }
    
    NSUInteger rangeLength = range.location;
    return rangeLength;
}

-(NSUInteger) getActualMaximum:(JavaCalendarFieldType) fieldType{
    NSRange range;
    switch (fieldType) {
        case JavaCalendarFieldTypeYear:
            range = NSMakeRange(1970, 2100);
            break;
        case JavaCalendarFieldTypeMonth:
            range = [calendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:internalDate];
            break;
        case JavaCalendarFieldTypeDate:
            range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:internalDate];
            break;
        case JavaCalendarFieldTypeHourOfDay:
            range = [calendar rangeOfUnit:NSCalendarUnitHour inUnit:NSCalendarUnitDay forDate:internalDate];
            break;
        case JavaCalendarFieldTypeMinute:
            range = [calendar rangeOfUnit:NSCalendarUnitMinute inUnit:NSCalendarUnitHour forDate:internalDate];
            break;
        case JavaCalendarFieldTypeSecond:
            range = [calendar rangeOfUnit:NSCalendarUnitSecond inUnit:NSCalendarUnitMinute forDate:internalDate];
            break;
    }
    
    NSUInteger rangeLength = range.length;
    if(fieldType == JavaCalendarFieldTypeHourOfDay){
        //这里发现一个问题，获取一天最大的小时数，会返回24，这是不正确的，应该返回23才对
        rangeLength-=1;
    }
    return rangeLength;
}

@end
